/* Copyright 2010 Anthony Surma
This file is part of Java's Kademlia Extension (JKADX).

JKADX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

JKADX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with JKADX.  If not, see <http://www.gnu.org/licenses/>.
*/

package jkadx.hashCollisions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.security.*;
import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author Anthony Surma
 */
public class hashValueGenerator
{   
    public static void main(String[] args) throws Exception
    {
        final String input;
        final String ALGORITHM     = "SHA1";
        final Charset  CHARSET     = Charset.forName("UTF-8");
        final byte[] hashkey;
        final byte[] verify;
        final File saveFile        = new File("hash.key");
        final FileOutputStream fos = new FileOutputStream(saveFile);
        final FileInputStream  fis;
        final String ONE           = "1";
        final String ZERO          = "0";
        final StringBuilder sb;
        byte[] test;

        System.out.print("Enter text to run through cryptographically secure hash function:\n> ");

        input   = new Scanner(System.in).nextLine();
        hashkey = MessageDigest.getInstance(ALGORITHM).digest(test = input.getBytes(CHARSET));
        verify  = new byte[hashkey.length];
        sb      = new StringBuilder(Byte.SIZE * hashkey.length);

        System.out.println("Test length: " + test.length);
        System.out.println("Test[0]: " + test[0]);
        for (byte b : hashkey)
        {
            for (int i = 0; i < Byte.SIZE; ++i)
            {                
                if (((b << i) & 0x80) == 0x80) // this is definately the correct way
                {
                    sb.append(ONE);
                }
                else
                {
                    sb.append(ZERO);
                }                
            }
        }
        System.out.print("Generated " + (hashkey.length * Byte.SIZE) + "-bit hashkey: " + sb.toString());
        saveFile.delete();
        fos.write(hashkey);
        fos.flush();

        //fos.close();
        System.out.println("\nSaved " + hashkey.length + " bytes to file: " + saveFile.getAbsolutePath());
        fis = new FileInputStream(saveFile);
        fis.read(verify);
        fis.close();
        if (Arrays.equals(hashkey, verify))
        {
             System.out.println("File contents verified as readable.");
        }
        else
        {
            System.out.println("FATAL ERROR: There was a problem verifying the file contents!");
        }
    }   
}
